package com.autumn.zero.listing.configure;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.autumn.zero.listing.DefaultAutumnZeroListingCallbackContext;
import com.autumn.zero.listing.IAutumnZeroListingCallback;
import com.autumn.zero.listing.IAutumnZeroListingCallbackContext;

/**
 * 列表配置
 * 
 * @author 杨昌国 2018-12-13 16:16:43
 */
@Configuration
public class AutumnZeroListingConfiguration {

	/**
	 * 
	 * @return
	 */
	@Bean
	@ConditionalOnMissingBean(IAutumnZeroListingCallbackContext.class)
	public IAutumnZeroListingCallbackContext autumnZeroListingCallbackContext() {
		return new DefaultAutumnZeroListingCallbackContext();
	}

	/**
	 * 拦截
	 * 
	 * @return
	 */
	@Bean
	public BeanPostProcessor autumnZeroListingCallbackContextBeanPostProcessor(
			IAutumnZeroListingCallbackContext zeroListingCallbackContext) {
		return new BeanPostProcessor() {
			@Override
			public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
				if (bean instanceof IAutumnZeroListingCallback) {
					IAutumnZeroListingCallback listingCallback = (IAutumnZeroListingCallback) bean;
					zeroListingCallbackContext.registerListingCallback(listingCallback);
				}
				return bean;
			}

			@Override
			public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
				return bean;
			}
		};
	}

}
